package com.btl.cygf;

import java.util.HashMap;
import java.util.Map;

import com.btl.http.HttpServies;
import com.btl.utils.MD5Encrypt;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class UpdatePass extends Activity implements OnClickListener{

	private EditText old_pass;
	private EditText new_pass1;
	private EditText new_pass2;
	
	private String result;
	
	private boolean value;
	private TextView submit;
	private ImageView login_backtofragment;
	
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0x1123) {
				if (result.equals("账号或密码不正确")) {
					Toast.makeText(getApplicationContext(), "原密码不正确", 1).show();
					new_pass1.setText("");
					new_pass2.setText("");
					old_pass.setText("");
				} else if (result.equals("封号状态")) {
					Toast.makeText(getApplicationContext(), result, 1).show();
					finish();
				} else {
					getUserInfo();
				}

			}
			
			if(msg.what == 0x1125) {
				if (value) {
					Toast.makeText(getApplicationContext(), "修改成功", 1).show();
				} else {
					Toast.makeText(getApplicationContext(), "修改失败", 1).show();
				}
				
				finish();
			}

			

		}

		

	};


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.update_pass);
		initView();
	}

	private void initView() {

		old_pass = (EditText) findViewById(R.id.old_pass);
		new_pass1 = (EditText) findViewById(R.id.new_pass1);
		new_pass2 = (EditText) findViewById(R.id.new_pass2);
		
		login_backtofragment = (ImageView) findViewById(R.id.login_backtofragment);
		submit = (TextView) findViewById(R.id.submit);
		login_backtofragment.setOnClickListener(this);
		submit.setOnClickListener(this);

	}

	private void submit() {
		
		System.out.println("tijiao");
		SharedPreferences share = getSharedPreferences("login",
				Activity.MODE_WORLD_READABLE);

		final String id = share.getString("id", "");
		
		new Thread(new Runnable() {
			@Override
			public void run() {

				Map<String, String> params = new HashMap<>();
				params.put("id", id);
				params.put("pass", MD5Encrypt.getMD5Str(old_pass.getText().toString()));

				try {
					result = (String) HttpServies.SendPostRequest(
							getResources().getString(R.string.url)
									+ "/CYGF/loginVerification.user", params,
							"utf-8", 2);
					System.out.println(result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				handler.sendEmptyMessage(0x1123);
			}
		}).start();
	}

	public void fanhui(View v) {
		finish();
	}
	
	private void getUserInfo() {
		final SharedPreferences share = getSharedPreferences("login",
				Activity.MODE_WORLD_READABLE);
		final String id = share.getString("id", "");
		if(new_pass1.getText().toString().equals(new_pass2.getText().toString())){
			new Thread(new Runnable() {
				@Override
				public void run() {

					Map<String, String> params = new HashMap<>();
					params.put("id", id);
					params.put("param", MD5Encrypt.getMD5Str(new_pass1.getText().toString()));
					params.put("type", "pass");

					try {
						value = (boolean) HttpServies.SendPostRequest(
								getResources().getString(R.string.url)
										+ "/CYGF/updatePerson.user", params,
								"utf-8", 1);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					handler.sendEmptyMessage(0x1125);
				}
			}).start();
		} else {
			Toast.makeText(getApplicationContext(), "两次输入不一致", 1).show();
		}
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit:
			submit();
			break;
		case R.id.login_backtofragment:
			finish();
			break;

		default:
			break;
		}
		
	}

}
